package com.mt.skyhoist.ui.activity;

import static com.mt.skyhoist.ui.activity.ScanDeviceService.SelectedChange;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;


import com.mt.skyhoist.R;
import com.mt.skyhoist.util.GlobalVariable;
import com.mt.skyhoist.util.user.MixDatasModelSource;
import com.mt.skyhoist.util.view.GroupView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;


public class Connect2Fragment extends Fragment {
	private GlobalVariable mGlobalVariable;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mGlobalVariable = GlobalVariable.getinstance();
		initData();
	}

	private void initData(){

		if(mGlobalVariable.groupList != null){
			return;
		}

		mGlobalVariable.groupList = new ArrayList<>();

		for (int i=0; i<250; i++) {
			GroupView.GroupFmtBean groupFmtBean = new GroupView.GroupFmtBean();
			groupFmtBean.setTag(i);

			mGlobalVariable.groupList.add(groupFmtBean);
		}

		if(mGlobalVariable.getMixDatas().getSelectgrounp() != -1){  // 有编组被选中
			mGlobalVariable.groupList.get(mGlobalVariable.getMixDatas().getSelectgrounp()).setToggleButtonEnable(true);
		}
	}

	private View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (view != null) {
			return view;
		}

		view = inflater.inflate(R.layout.fragment_connect2, container, false);

		initView();

		return view;
	}

	private ListView lv;
	private void initView(){
		lv = (ListView) view.findViewById(R.id.lv);

		lv.setAdapter(listadapter);
	}

	private BaseAdapter listadapter = new BaseAdapter() {
		@Override
		public int getCount() {
			return mGlobalVariable.groupList.size();
		}

		@Override
		public GroupView.GroupFmtBean getItem(int position) {
			return mGlobalVariable.groupList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (null == convertView) {
				convertView = new GroupView(Connect2Fragment.this.getContext());
			}

			((GroupView) convertView).setData(getItem(position));
			((GroupView) convertView).setCallback(groupCallback);

			return convertView;
		}
	};


	private GroupView.Callback groupCallback = new GroupView.Callback() {
		@Override
		public void onToggleClick(GroupView.GroupFmtBean data, boolean enable) {
			mGlobalVariable.groupList.get(data.getTag()).setToggleButtonEnable(enable);

			if(!enable){  // 取消选中时
				mGlobalVariable.groupList.get(mGlobalVariable.getMixDatas().getSelectgrounp()).setAssignButtonEnable(false);
				mGlobalVariable.getMixDatas().setSelectgrounp(-1);
				listadapter.notifyDataSetChanged();
				connectFragmentCallback.statuesChange(data.getTag(), false);
			} else {   // 选中时
				if(mGlobalVariable.getMixDatas().getSelectgrounp() != -1){  // 如果已经有被选中的组，则需要取消掉
					System.out.println("Truth 有已经存在的选中->"+mGlobalVariable.getMixDatas().getSelectgrounp());
					mGlobalVariable.groupList.get(mGlobalVariable.getMixDatas().getSelectgrounp()).setToggleButtonEnable(false);
					mGlobalVariable.groupList.get(mGlobalVariable.getMixDatas().getSelectgrounp()).setAssignButtonEnable(false);
					listadapter.notifyDataSetChanged();
					connectFragmentCallback.statuesChange(mGlobalVariable.getMixDatas().getSelectgrounp(), false);
				}

				mGlobalVariable.getMixDatas().setSelectgrounp(data.getTag());
				connectFragmentCallback.statuesChange(data.getTag(), true);
				listadapter.notifyDataSetChanged();
			}

			MixDatasModelSource.getInstance().update(mGlobalVariable.getMixDatas());

			EventBus.getDefault().post(SelectedChange);
		}

		@Override
		public void onNameLongClick(GroupView.GroupFmtBean data) {
			connectFragmentCallback.bindNewDevice(data.getTag());
		}
	};

	// 设置连接状态
	public void setConnectStatues(int index, boolean statues) {
		mGlobalVariable.groupList.get(index).setAssignButtonEnable(statues);
		listadapter.notifyDataSetChanged();
	}

	private ConnectFragmentCallback connectFragmentCallback;
	public void setCallback(ConnectFragmentCallback connectFragmentCallback){
		this.connectFragmentCallback = connectFragmentCallback;
	}
	public interface ConnectFragmentCallback{
		void bindNewDevice(int index);
		void statuesChange(int index, boolean statues);
	}

}
